﻿using System;
using System.Data;
using System.Reflection;
using System.Text;
using System.Web;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using AnyOffice.Model;
using log4net;
using Microsoft.ApplicationBlocks.Data;
namespace AnyOffice.general.system.user
{
    public partial class user_edit : ModulePage
    {
        private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);     
        protected string strTemp;
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!this.Page.IsPostBack)
            {
                ListItem item;
                int num = (Request.QueryString["dept_id"] == null) ? 0 : int.Parse(Request.QueryString["dept_id"].ToString());
                this.ViewState["DeptId"] = num;
                string str = (Request.QueryString["user_id"] == null) ? "" : Request.QueryString["user_id"].ToString();
                this.ViewState["UserId"] = str;
                DataTable table = new DepartmentDao().SelectParentAll(base.Conn);
                if (table.Rows.Count > 0)
                {
                    for (int i = 0; i < table.Rows.Count; i++)
                    {
                        item = new ListItem("|─" + table.Rows[i]["DeptName"].ToString(), table.Rows[i]["DeptId"].ToString());
                        this.ddlDeptId.Items.Add(item);
                        this.strTemp = "";
                        this.GetDept(ref this.ddlDeptId, int.Parse(table.Rows[i]["DeptId"].ToString()), this.strTemp);
                    }
                }
                this.ddlDeptId.Items.Add(new ListItem("离职人员/外部人员", "-1"));
                DataTable table2 = new UserPrivDao().SelectAll(base.Conn);
                if (table2.Rows.Count > 0)
                {
                    for (int j = 0; j < table2.Rows.Count; j++)
                    {
                        item = new ListItem(table2.Rows[j]["PrivName"].ToString(), table2.Rows[j]["PrivId"].ToString());
                        this.ddlUserPriv.Items.Add(item);
                    }
                }
                //this.ddlDutyTypeId.DataSource = this.GetdDutyData();
                //this.ddlDutyTypeId.DataBind();
                DataSet set = new DataSet();
                //set.ReadXml(Server.MapPath("/theme2009/theme.xml"));
                //this.ddlTheme.DataSource = set.Tables["Theme"];
                //this.ddlTheme.DataValueField = "Id";
                //this.ddlTheme.DataTextField = "Name";
                //this.ddlTheme.DataBind();
                DataTable table3 = new UsersDao().SelectByUserId(base.Conn, str);
                if (table3.Rows.Count == 0)
                {
                    this.pnlMain.Visible = false;
                    this.litMessage.Text = base.ShowError("无此用户！", "user_dept.aspx?dept_id=" + num.ToString());
                    return;
                }
                this.lblUserId.Text = str;
                if (table3.Rows[0]["UserNo"] != DBNull.Value)
                {
                    this.txtUserNo.Text = table3.Rows[0]["UserNo"].ToString();
                }
                if (table3.Rows[0]["UserName"] != DBNull.Value)
                {
                    this.txtUserName.Text = table3.Rows[0]["UserName"].ToString();
                }
                if (this.ddlSex.Items.FindByValue(table3.Rows[0]["Sex"].ToString().ToLower()) != null)
                {
                    this.ddlSex.Items.FindByValue(table3.Rows[0]["Sex"].ToString().ToLower()).Selected = true;
                }
                if (this.ddlDeptId.Items.FindByValue(table3.Rows[0]["DeptId"].ToString().ToLower()) != null)
                {
                    this.ddlDeptId.Items.FindByValue(table3.Rows[0]["DeptId"].ToString().ToLower()).Selected = true;
                }
                if (this.ddlUserPriv.Items.FindByValue(table3.Rows[0]["PrivId"].ToString()) != null)
                {
                    this.ddlUserPriv.Items.FindByValue(table3.Rows[0]["PrivId"].ToString()).Selected = true;
                }
                if (this.ddlPostPriv.Items.FindByValue(table3.Rows[0]["PostPriv"].ToString()) != null)
                {
                    this.ddlPostPriv.Items.FindByValue(table3.Rows[0]["PostPriv"].ToString()).Selected = true;
                }
                if (table3.Rows[0]["PostDept"] != DBNull.Value)
                {
                    this.ToId.Value = table3.Rows[0]["PostDept"].ToString();
                    if ((table3.Rows[0]["PostDeptNames"] != DBNull.Value) && (table3.Rows[0]["PostDeptNames"].ToString() != string.Empty))
                    {
                        this.ToName.Value = table3.Rows[0]["PostDeptNames"].ToString() + ",";
                    }
                }
                //if (this.ddlDutyTypeId.Items.FindByValue(table3.Rows[0]["DutyType"].ToString()) != null)
                //{
                //    this.ddlDutyTypeId.Items.FindByValue(table3.Rows[0]["DutyType"].ToString()).Selected = true;
                //}
                //if (table3.Rows[0]["AttendCard"] != DBNull.Value)
                //{
                //    this.txtAttendCard.Text = table3.Rows[0]["AttendCard"].ToString();
                //}
                this.txtEmailCapacity.Text = (table3.Rows[0]["EmailCapacity"] != DBNull.Value) ? table3.Rows[0]["EmailCapacity"].ToString() : "0";
                this.txtFolderCapacity.Text = (table3.Rows[0]["FolderCapacity"] != DBNull.Value) ? table3.Rows[0]["FolderCapacity"].ToString() : "50";
                //this.ckbUsingKey.Checked = Convert.ToBoolean(table3.Rows[0]["UsingKey"].ToString());
                if (table3.Rows[0]["BindIp"] != DBNull.Value)
                {
                    this.txtBindIp.Text = table3.Rows[0]["BindIp"].ToString();
                }
                if (Convert.ToBoolean(table3.Rows[0]["Disabled"].ToString()))
                {
                    this.ckbDisable.Checked = true;
                }
                if (table3.Rows[0]["UserId2"] != DBNull.Value)
                {
                    this.txtUserId2.Text = table3.Rows[0]["UserId2"].ToString();
                }
                if (this.ddlSex.Items.FindByValue(table3.Rows[0]["Sex"].ToString()) != null)
                {
                    this.ddlSex.Items.FindByValue(table3.Rows[0]["Sex"].ToString()).Selected = true;
                }
                DateTime time = Convert.ToDateTime(table3.Rows[0]["Birthday"].ToString());
                this.ddlBirthday_Year.Items.FindByValue(time.Year.ToString()).Selected = true;
                this.ddlBirthday_Mon.Items.FindByValue(time.Month.ToString()).Selected = true;
                this.ddlBirthday_Day.Items.FindByValue(time.Day.ToString()).Selected = true;
                if (Convert.ToBoolean(table3.Rows[0]["IsHideBirthday"].ToString()))
                {
                    this.ckbIsHideBirthday.Checked = true;
                }
                //if (this.ddlTheme.Items.FindByValue(table3.Rows[0]["Theme"].ToString()) != null)
                //{
                //    this.ddlTheme.Items.FindByValue(table3.Rows[0]["Theme"].ToString()).Selected = true;
                //}
                if (table3.Rows[0]["Mobile"] != DBNull.Value)
                {
                    this.txtMobile.Text = table3.Rows[0]["Mobile"].ToString();
                }
                if (Convert.ToBoolean(table3.Rows[0]["IsHideMobile"].ToString()))
                {
                    this.ckbIsHideMobile.Checked = true;
                }
                if (Convert.ToInt32(table3.Rows[0]["PostPriv"].ToString()) == 2)
                {
                    StringBuilder builder = new StringBuilder("<script>\n");
                    builder.Append("select_dept();\n");
                    builder.Append("</script>\n");
                    if (!this.Page.IsStartupScriptRegistered("dept_list"))
                    {
                        this.Page.RegisterStartupScript("dept_list", builder.ToString());
                    }
                }
            }
            if (this.Page.IsPostBack)
            {
                this.pnlMain.Visible = false;
                UsersDao dao4 = new UsersDao();
                UsersData data = new UsersData();
                data.UserId = this.ViewState["UserId"].ToString();
                data.UserNo = Convert.ToInt32(this.txtUserNo.Text);
                data.UserName = this.txtUserName.Text;
                data.DeptId = Convert.ToInt32(this.ddlDeptId.SelectedValue);
                data.PrivId = Convert.ToInt32(this.ddlUserPriv.SelectedValue);
                data.PostPriv = this.ddlPostPriv.Value;
                if (this.ddlPostPriv.Value == "2")
                {
                    data.PostDept = this.ToId.Value;
                }
                data.DutyType =1;
                
                    data.AttendCard = "";
                
                data.EmailCapacity = Convert.ToInt32(this.txtEmailCapacity.Text);
                data.FolderCapacity = Convert.ToInt32(this.txtFolderCapacity.Text);
                data.UsingKey = false;
                if (this.txtBindIp.Text != string.Empty)
                {
                    data.BindIp = this.txtBindIp.Text;
                }
                data.Sex = Convert.ToBoolean(this.ddlSex.SelectedValue);
                data.Birthday = Convert.ToDateTime(this.ddlBirthday_Year.Value + "-" + this.ddlBirthday_Mon.Value + "-" + this.ddlBirthday_Day.Value);
                data.IsHideBirthday = this.ckbIsHideBirthday.Checked;
                data.Theme =1;
                if (this.txtMobile.Text != string.Empty)
                {
                    data.Mobile = this.txtMobile.Text;
                }
                data.IsHideMobile = this.ckbIsHideMobile.Checked;
                if (this.ckbDisable.Checked)
                {
                    data.Disabled = true;
                }
                try
                {
                    dao4.Update(base.Conn, data);
                    if (this.txtUserId2.Text == this.ViewState["UserId"].ToString())
                    {
                        this.litUserId2.Text = base.ShowMessage("用户名和别名不能相同");
                    }
                    else if (!dao4.SetUserId2(base.Conn, this.ViewState["UserId"].ToString(), this.txtUserId2.Text))
                    {
                        this.litUserId2.Text = base.ShowMessage("用户名或别名 " + this.txtUserId2.Text + " 已存在");
                    }
                    string str2 = this.txtUserName.Text + ",UserId=" + this.ViewState["UserId"].ToString();
                    SysLog.Log(base.Conn, 7, str2);
                    UserCache.Update(this.ViewState["UserId"].ToString());
                }
                catch (Exception exception)
                {
                    if (log.IsErrorEnabled)
                    {
                        log.Error("", exception);
                    }
                    this.litMessage.Text = base.ShowError("编辑用户失败！", "user_dept.aspx?dept_id=" + this.ViewState["DeptId"].ToString());
                    return;
                }
                this.litMessage.Text = base.ShowPrompt("编辑用户成功！", "user_dept.aspx?dept_id=" + this.ViewState["DeptId"].ToString());
            }
        }
        private void GetDept(ref DropDownList ddlParentDept, int DeptId, string strTemp)
        {
            DataTable table = new DepartmentDao().SelectChildAll(base.Conn, DeptId);
            if (table.Rows.Count > 0)
            {
                strTemp = strTemp + "|　";
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    ListItem item = new ListItem(strTemp + "|─" + table.Rows[i]["DeptName"].ToString(), table.Rows[i]["DeptId"].ToString());
                    ddlParentDept.Items.Add(item);
                    this.GetDept(ref ddlParentDept, int.Parse(table.Rows[i]["DeptId"].ToString()), strTemp);
                }
            }
        }
        private DataTable GetdDutyData()
        {
            string commandText = "SELECT [DutyTypeId],[DutyName] FROM [dbo].[Attend_Config] ORDER BY [DutyTypeId] ASC";
            DataSet set = SqlHelper.ExecuteDataset(base.Conn, CommandType.Text, commandText);
            set.Tables[0].TableName = "Attend_Config";
            return set.Tables[0];
        }
    }
}
